﻿Imports System.IO

Public Class frmOptocontrol

    Private Sub frmOptocontrol_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        Dim ArchivosActuales As String() = Directory.GetFiles("\\websvr\ArchivosTXT")
        For Each archivoDA As String In ArchivosActuales
            lstArchivos.Items.Add(archivoDA.ToString())
        Next

        lblActual.Text = ""

    End Sub
    Sub Comenzar()
        Dim Seleccionado As String = ""
        Dim TOTAL As Integer = 0
        Dim Selected As String = ""

        For i = 0 To lstArchivos.Items.Count - 1
            Seleccionado = lstArchivos.Items(i).ToString
            TOTAL = Len(Seleccionado)
            Selected = Mid(Seleccionado, TOTAL - 6, 3)
            Procesar(Selected, Seleccionado)
        Next

        lstArchivos.Items.Clear()
        lblActual.Text = "Listo"

    End Sub
    Sub Procesar(ByVal Tipo As String, ByVal Fichero As String)
        Select Case UCase(Tipo)
            Case "CON"
                CONDUCTORES(Fichero)
            Case "HIS"
                HISTORICO_RECAUDACION(Fichero)
            Case "SEC"
                SECTORES(Fichero)
            Case "VEH"
                VEHICULOS(Fichero)
            Case "ZON"
                ZONAS(Fichero)
            Case "REG"
                REGISTROS(Fichero)

        End Select
    End Sub
    Sub REGISTROS(ByVal Fichero As String)
        Dim REGISTRO As New clsREGISTRO
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim Cont As Integer = 0

        Dim IdAutobus As Integer = 0
        Dim IdAcumulado As Integer = 0
        Dim IdDetalle As Integer = 0
        Dim IdMensual As Integer = 0
        Dim Fecha As Date = Now()
        Dim strFecha As String = ""
        Dim ID_VEHICULO As String = "?"
        Dim Primero As Boolean = True
        Dim Kilometros As Double = 0

        Dim Acumulado As New clsKmsAcumulado
        Dim Detalle As New clsKmsDetalle
        Dim Mensual As New clsKmsMensuales


        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")
                REGISTRO.ID_VEHICULO = Cadena(1)
                REGISTRO.ID_REGISTRO = Cadena(0)
                REGISTRO.FECHA_HORA = CDate(Cadena(2))
                REGISTRO.FECHA = CDate(Cadena(3))
                REGISTRO.HORA = CDate(Cadena(4))
                REGISTRO.SUBIDAS = Cadena(5)
                REGISTRO.BAJADAS = Cadena(6)
                REGISTRO.BLOQUEOS = Cadena(7)
                REGISTRO.LONGITUD = Cadena(8)
                REGISTRO.LATITUD = Cadena(9)
                REGISTRO.DISTANCIA = Cadena(12)
                REGISTRO.VELOCIDAD = Cadena(14)
                REGISTRO.EVENTOS = Cadena(13)
                REGISTRO.ID_PUERTA = Cadena(15)
                REGISTRO.ABORDO = Cadena(16)
                REGISTRO.LUGAR = Cadena(26)
                REGISTRO.ES_TERMINAL = Cadena(27)

                If REGISTRO.ID_VEHICULO <> ID_VEHICULO Then
                    If Primero = False Then
                        IdAutobus = BuscarIntDatoCitra("SELECT IdAutobus FROM Autobuses WHERE UPPER(Texto02)='" & UCase(ID_VEHICULO) & "' AND Estatus=1")
                        If IdAutobus > 0 Then
                            'PRIMERO ACTUALIZA ACUMULADO*********************************************************************
                            IdAcumulado = BuscarIntDatoCitra("SELECT IdAcumulado FROM KmsAcumulado WHERE IdAutobus=" & IdAutobus)
                            If IdAcumulado > 0 Then
                                'YA EXISTE ACUMULADO
                                Acumulado.IdAutobus = IdAutobus
                                Acumulado.KmsAcumulados = BuscarDblDatoCitra("SELECT KmsAcumulados FROM KmsAcumulado WHERE IdAcumulado=" & IdAcumulado) + Kilometros
                                Acumulado.IdAcumulado = IdAcumulado
                                Acumulado.Actualizar()

                            Else
                                'NO EXISTE ACUMULADO
                                Acumulado.IdAutobus = IdAutobus
                                Acumulado.KmsAcumulados = Kilometros
                                Acumulado.Insertar()

                            End If
                            'AHORA ACTUALIZA KMS MENSUALES***********************************************************************
                            IdMensual = BuscarIntDatoCitra("SELECT IdMensuales FROM KmsMensuales WHERE IdAutobus=" & IdAutobus & " AND Mes=" & Fecha.Month & " AND ANIO=" & Fecha.Year)
                            If IdMensual > 0 Then
                                Mensual.IdMensual = IdMensual
                                Mensual.IdAutobus = IdAutobus
                                Mensual.KmsAcumulados = BuscarDblDatoCitra("SELECT KmsAcumulados FROM KmsMensuales WHERE IdAutobus=" & IdAutobus & " AND Mes=" & Fecha.Month & " AND ANIO=" & Fecha.Year) + Kilometros
                                Mensual.KmsTrabajados = BuscarDblDatoCitra("SELECT KmsTrabajados FROM KmsMensuales WHERE IdAutobus=" & IdAutobus & " AND Mes=" & Fecha.Month & " AND ANIO=" & Fecha.Year) + Kilometros
                                Mensual.KmsMuertos = BuscarDblDatoCitra("SELECT KmsMuertos FROM KmsMensuales WHERE IdAutobus=" & IdAutobus & " AND Mes=" & Fecha.Month & " AND ANIO=" & Fecha.Year) + Kilometros
                                Mensual.Actualizar()
                            Else
                                Mensual.IdAutobus = IdAutobus
                                Mensual.Mes = Fecha.Month
                                Mensual.Anio = Fecha.Year
                                Mensual.KmsAcumulados = Kilometros
                                Mensual.KmsTrabajados = 0
                                Mensual.KmsMuertos = 0
                                Mensual.Insertar()
                            End If
                            'AHORA INSERTA KILOMETROS DEL DIA
                            IdDetalle = BuscarIntDatoCitra("SELECT IdDetalle FROM KmsDetalle WHERE IdAutobus=" & IdAutobus & " AND CONVERT(VARCHAR,Fecha,101)='" & strFecha & "'")
                            If IdDetalle > 0 Then
                                Detalle.IdDetalle = IdDetalle
                                Detalle.Kms = Kilometros
                                Detalle.KmsMuertos = Kilometros - BuscarDblDatoCitra("SELECT  KmsTrabajados FROM KmsDetalle WHERE IdDetalle=" & IdDetalle)
                                Detalle.ActualizarFROMREGISTRO()
                            Else
                                Detalle.IdAutobus = IdAutobus
                                Detalle.Fecha = Fecha
                                Detalle.Kms = Kilometros
                                Detalle.KmsTrabajados = 0
                                Detalle.KmsMuertos = 0
                                Detalle.Insertar()
                            End If
                            
                        End If
                        Kilometros = 0


                    End If
                End If


                ID_VEHICULO = REGISTRO.ID_VEHICULO
                Kilometros = Kilometros + REGISTRO.DISTANCIA
                Primero = False
                Fecha = REGISTRO.FECHA
                strFecha = Fecha.ToString("MM/dd/yyyy")

                REGISTRO.Insertar()
                Cont = Cont + 1

                'ProgressBar1.Value = Value
                lblActual.Text = "Importando " & Cont & " registros..."
                Application.DoEvents()

            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)

    End Sub
    Sub ZONAS(ByVal Fichero As String)
        Dim ZONA As New clsZONA
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim ID As String
        Dim Cont As Integer = 0

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")
                ZONA.ID_ZONA = Cadena(0)
                ZONA.COD_ZONA = Cadena(1)

                ID = BuscarStrDatoOptoControl("SELECT COD_ZONA FROM ZONA WHERE ID_ZONA='" & ZONA.ID_ZONA & "'")
                If ID = "" Then
                    ZONA.Insertar()
                Else
                    ZONA.Actualizar()
                End If


                Cont = Cont + 1

                'ProgressBar1.Value = Value
                lblActual.Text = "Importando " & Cont & " zona(s)..."
                Application.DoEvents()

            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)
    End Sub
    Sub VEHICULOS(ByVal Fichero As String)
        Dim VEHICULO As New clsVEHICULO
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim ID As String
        Dim Cont As Integer = 0

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")
                VEHICULO.ID_VEHICULO = Cadena(0)
                VEHICULO.COD_VEHICULO = Cadena(1)
                VEHICULO.NUMERO_ESCLAVO = Cadena(2)
                VEHICULO.ID_SECTOR = Cadena(9)
                VEHICULO.IP_CLIENTE = Cadena(25)

                ID = BuscarStrDatoOptoControl("SELECT NUMERO_ESCLAVO FROM VEHICULO WHERE ID_VEHICULO='" & VEHICULO.ID_VEHICULO & "'")
                If ID = "" Then
                    VEHICULO.Insertar()
                Else
                    VEHICULO.Actualizar()
                End If


                Cont = Cont + 1

                'ProgressBar1.Value = Value
                lblActual.Text = "Importando " & Cont & " vehiculo(s)..."
                Application.DoEvents()

            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)
    End Sub
    Sub SECTORES(ByVal Fichero As String)
        Dim SECTOR As New clsSECTOR
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim ID As String
        Dim Cont As Integer = 0

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")
                SECTOR.ID_SECTOR = Cadena(0)
                SECTOR.COD_SECTOR = Cadena(1)
                SECTOR.NOMBRE_EMPRESA = Cadena(7)

                ID = BuscarStrDatoOptoControl("SELECT COD_SECTOR FROM SECTOR WHERE ID_SECTOR='" & SECTOR.ID_SECTOR & "'")
                If ID = "" Then
                    SECTOR.Insertar()
                Else
                    SECTOR.Actualizar()
                End If

                Cont = Cont + 1

                ' ProgressBar1.Value = Value
                lblActual.Text = "Importando " & Cont & " sector(es)..."
                Application.DoEvents()

            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)
    End Sub
    Sub HISTORICO_RECAUDACION(ByVal Fichero As String)
        Dim HISTORICO As New clsHISTORICO
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim Cont As Integer = 0
        Dim IdDetalle As Integer = 0
        Dim Detalle As New clsKmsDetalle
        Dim IdAutobus As Integer
        Dim strFecha As String = ""
        Dim Fecha As Date
        Dim Kilometros As Double = 0
        Dim Primero As Boolean = True
        Dim ID_VEHICULO As String = "?"

        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")

                HISTORICO.ID_HISTORICO_RECAUDACION = Cadena(0)
                HISTORICO.ID_VEHICULO = Cadena(1)
                HISTORICO.ID_RUTA = Cadena(2)
                HISTORICO.ID_CONDUCTOR = Cadena(3)
                HISTORICO.FECHA_HORA_INICIO = CDate(Cadena(4))
                HISTORICO.FECHA_HORA_FIN = CDate(Cadena(5))
                HISTORICO.MINUTOS = Cadena(6)
                HISTORICO.PASAJEROS = Cadena(7)
                HISTORICO.MONTO = Cadena(8)
                HISTORICO.MARCAS_DESCONTADAS = Cadena(10)
                HISTORICO.MONTO_MARCAS_DESCONTADAS = Cadena(11)
                HISTORICO.MONTO_DEDUCCIONES = Cadena(12)
                HISTORICO.BLOQUEOS = Cadena(13)
                HISTORICO.KMS = Cadena(14)
                HISTORICO.VUELTAS = Cadena(15)
                HISTORICO.MONTO_MENOS_DEDUCCIONES = Cadena(17)
                HISTORICO.ID_SECTOR = Cadena(19)
                HISTORICO.ID_ZONA = Cadena(26)
                HISTORICO.FECHA = CDate(Cadena(27))

                If HISTORICO.ID_VEHICULO <> ID_VEHICULO Then
                    If Primero = False Then
                        IdAutobus = BuscarIntDatoCitra("SELECT IdAutobus FROM Autobuses WHERE UPPER(Texto02)='" & UCase(ID_VEHICULO) & "' AND Estatus=1")
                        If IdAutobus > 0 Then
                            IdDetalle = BuscarIntDatoCitra("SELECT IdDetalle FROM KmsDetalle WHERE IdAutobus=" & IdAutobus & " AND CONVERT(VARCHAR,Fecha,101)='" & strFecha & "'")
                            If IdDetalle > 0 Then
                                Detalle.IdDetalle = IdDetalle
                                Detalle.KmsTrabajados = Kilometros
                                Detalle.KmsMuertos = BuscarDblDatoCitra("SELECT  Kms FROM KmsDetalle WHERE IdDetalle=" & IdDetalle) - Kilometros
                                Detalle.ActualizarFROMHISTORICO()
                            Else
                                Detalle.IdAutobus = IdAutobus
                                Detalle.Fecha = Fecha
                                Detalle.Kms = Kilometros
                                Detalle.KmsTrabajados = 0
                                Detalle.KmsMuertos = 0
                                Detalle.Insertar()
                            End If
                        End If
                    End If
                End If

                Kilometros = Kilometros + HISTORICO.KMS
                Fecha = HISTORICO.FECHA
                strFecha = Fecha.ToString("MM/dd/yyyy")

                

                HISTORICO.Insertar()
                Cont = Cont + 1

                ' ProgressBar1.Value = Value
                lblActual.Text = "Importando " & Cont & " vuelta(s) analizada(s)..."
                Application.DoEvents()
            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)
    End Sub
    Sub CONDUCTORES(ByVal Fichero As String)
        Dim CONDUCTOR As New clsCONDUCTOR
        Dim objReader As New StreamReader(Fichero)
        Dim sLine As String = ""
        Dim Cadena As String()
        Dim ID As String
        Dim Cont As Integer = 0


        Do
            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then
                Cadena = sLine.Split("|")
                CONDUCTOR.ID_CONDUCTOR = Cadena(0)
                CONDUCTOR.COD_CONDUCTOR = Cadena(1)
                CONDUCTOR.NOMBRE = Cadena(2)
                CONDUCTOR.APELLIDO1 = Cadena(3)
                CONDUCTOR.APELLIDO2 = Cadena(4)
                CONDUCTOR.ID_SECTOR = Cadena(5)
                CONDUCTOR.ACTIVO = Cadena(6)
                CONDUCTOR.ID_CONDUCTOR_DVR = Cadena(7)

                ID = BuscarStrDatoOptoControl("SELECT COD_CONDUCTOR FROM CONDUCTOR WHERE ID_CONDUCTOR='" & CONDUCTOR.ID_CONDUCTOR & "'")
                If ID = "" Then
                    CONDUCTOR.Insertar()
                Else
                    CONDUCTOR.Actualizar()

                End If

                Cont = Cont + 1

                lblActual.Text = "Importando " & Cont & " conductor(es)..."
                Application.DoEvents()
            End If
        Loop Until sLine Is Nothing

        objReader.Dispose()
        objReader.Close()

        My.Computer.FileSystem.DeleteFile(Fichero)
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Comenzar()

    End Sub
End Class